#include<vector>
using namespace std;
class Solution {
public:
    vector<int> smallestK(vector<int>& nums, int k) {
        srand(time(NULL));
        qsort(nums, 0, nums.size() - 1, k);
        return { nums.begin(), nums.begin() + k };
    }

    int randrom(vector<int>& nums, int l, int r) {
        int ret = rand();
        return nums[ret % (r - l + 1) + l];
    }
    void qsort(vector<int>& nums, int l, int r, int k) {
        if (l >= r)
            return;
        int left = l - 1, right = r + 1, i = l;
        int key = randrom(nums, l, r);
        while (i < right) {
            if (nums[i] < key)
                swap(nums[i++], nums[++left]);
            else if (nums[i] > key)
                swap(nums[i], nums[--right]);
            else
                i++;
        }
        int a = left - l + 1, b = right - left - 1;
        if (a > k)
            qsort(nums, l, left, k);
        else if (a + b >= k)
            return;
        else
            qsort(nums, right, r, k - a - b);
    }
};